# -*- coding: utf-8 -*-

import os

N = 0


def file_name(file_dir):
    for root, dirs, files in os.walk(file_dir):
        return [root + '/' + file for file in files if ".xlsx" in file]


import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib.ticker as ticker
import numpy as np

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号


def file_plot(index, values, label, mkdir_name):
    print(values)
    
    def x_fmt_func(x, pos=None):
        idx = np.clip(int(x + 0.5), 0, len(index) - 1)
        return index[idx]
    
    fig = plt.figure(figsize=(24, 8), dpi=80)
    ax = fig.add_subplot(1, 1, 1)
    ax.set_ylim(15, 26)
    ax.set_yticks(range(15, 27))
    # 横坐标描述
    plt.xlabel('日期')
    
    # 纵坐标描述
    plt.ylabel('时间')
    
    ax.plot(index, values, label=label, color='#FF6A6A', linewidth=2, linestyle="--")
    for a, b in zip(index, values):
        plt.text(a, b, str(b).replace(".", ":"), ha='center', va='bottom', fontsize=14, color='#555555')
    
    plt.legend()
    plt.savefig("./images/{}/{}.png".format(mkdir_name, label))


import pandas as pd


def file_load(file):
    file_name = str(file).split('.xlsx')[0]
    file_name = str(file_name).split('/')[-1]
    try:
        os.mkdir("./images/{}".format(file_name))
    except Exception as e:
        pass
    d = pd.read_excel(file, names=[str(i) for i in range(47)])
    data = pd.DataFrame(d)
    data = data.rename(columns={'7': 'Start', '9': 'End', '0': 'Name', '5': 'Date'})
    data = data.loc[3:]
    data_start = data[['Start','Name','Date']]
    data_start = data.dropna(axis=0, how='any')
    data_end = data[['End', 'Name', 'Date']]
    data_end = data_end.dropna(axis=0, how='any')
    data_group = data_end.reset_index().groupby(["Name"])
    names = data_group.groups.keys()
    index_list = []
    have_foreach = []
    for name in names:
        for i in range(len(data_end)):
            if i in have_foreach:
                continue
            if str(data_end.iloc[i]['Name']) == name:
                index_list.append(i)
                data_end.iloc[i]['End'] = float(str(data_end.iloc[i]['End']).replace(":", ".")) if not "次日" in str(
                    data_end.iloc[i]['End']) else 24 + float(str(data_end.iloc[i]['End']).replace(":", ".")[-5:])
                data_end.iloc[i]['Date'] = "\n".join(str(data_end.iloc[i]['Date']).split(" "))
        index_max = max(index_list)
        index_min = min(index_list)
        have_foreach += index_list
        file_plot(data_end['Date'][index_min:index_max], data_end['End'][index_min:index_max], name, file_name)
        index_list = []
        # break
    # return data


if __name__ == '__main__':
    files = file_name("./files")
    for file in files:
        data = file_load(file)
        # print(data)
    # print(files)
